package com.niiwoo.civet.trade.service.local.creditor;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.niiwoo.civet.trade.dao.entity.CreditorRateConfig;
import com.niiwoo.civet.trade.dao.mapper.CreditorRateConfigMapperExt;
import com.niiwoo.civet.trade.dto.CreditorSaleOrderRateAreaDTO;

import lombok.extern.slf4j.Slf4j;

/**
 * 债权费率本地服务
 * Created by zhoulinquan on 2018/5/15.
 */
@Slf4j
@Service
public class CreditorRateConfigService {
	@Autowired
	private CreditorRateConfigMapperExt creditorRateConfigMapperExt;
	
	/**
	 * 查询所有的债转费率配置
	 * @return
	 */
	public List<CreditorRateConfig> getAllCreditorRateConfigService() {
		return creditorRateConfigMapperExt.getAllCreditorRateConfigService();
	}
	
	/**
	 * 根据未收期限查询对应的债转费率
	 * @param restPeriod
	 * @return
	 */
	public CreditorRateConfig getCreditorRateConfigService(Integer restPeriod, Byte repaymentType) { 
		List<CreditorRateConfig> list = creditorRateConfigMapperExt.getCreditorRateConfigService(restPeriod, repaymentType);
		
		if (list == null || list.isEmpty()) return null;
		
		return list.get(0);
	}

	/**
	 * 获取债转费率配置区间值
	 * @return
	 */
	public CreditorSaleOrderRateAreaDTO getCreditorSaleOrderRateArea() {
		List<CreditorRateConfig> list = creditorRateConfigMapperExt.getAllCreditorRateConfigService();
		
		if (list == null || list.isEmpty()) return null;
		
		BigDecimal minRate = null;
		BigDecimal maxRate = null;
		for (CreditorRateConfig c : list) {
			if (minRate == null) minRate = c.getTransferRate();
			if (maxRate == null) maxRate = c.getTransferRate();
			
			if (minRate.compareTo(c.getTransferRate()) > 0) minRate = c.getTransferRate();
			if (maxRate.compareTo(c.getTransferRate()) < 0) maxRate = c.getTransferRate();
		}
		
		CreditorSaleOrderRateAreaDTO result = new CreditorSaleOrderRateAreaDTO();
		result.setMaxRate(maxRate);
		result.setMinRate(minRate);
		return result;
	}
	
}
